package com.jkc.xh.activities;

import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.jkc.xh.R;
import com.jkc.xh.manager.UserManager;
import com.jkc.xh.models.SmsListInfo;
import com.jkc.xh.network.RestCallBack;
import com.jkc.xh.network.models.GetSmsListResponse;
import com.jkc.xh.network.models.LoginResponse;
import com.jkc.xh.network.models.RegisterRequest;
import com.jkc.xh.network.models.RestError;
import com.jkc.xh.utils.L;
import com.jkc.xh.views.widget.NavBar;
import com.jkc.xh.views.widget.ToastUtil;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
@SuppressWarnings("ResourceType")
@EActivity(R.layout.activity_register)
public class RegisterActivity extends BaseActivity {
    @ViewById
    EditText inputLoginName;
    @ViewById
    EditText inputPassword;
    @ViewById
    EditText inputPasswordAgain;
    @ViewById
    EditText inputPhone;
    @ViewById
    EditText inputRadomNum;
    @ViewById
    EditText inputQQ;
    @ViewById
    EditText inputRecommondId;
    @ViewById
    RadioGroup smsRgGroup;
    @ViewById
    TextView getSmsCodeView;

    @ViewById(R.id.nav_bar)
    NavBar mNavBar;

    private long selectSmsId = -1;

    @Click(R.id.getSmsCodeView)
    void getSmsCode() {
        String mobile = inputPhone.getText().toString();
        if (TextUtils.isEmpty(mobile) || mobile.length() != 11) {
            ToastUtil.showToastShort("请输入正确的手机号码");
            return;
        }
        showProgress("获取验证码中");
        UserManager.getSmsCode(mobile, 1, selectSmsId, new RestCallBack<Object>() {
            @Override
            public void success(Object o) {
                closeProgress();
                timer.start();
                ToastUtil.showToastShort("短信发送成功.请注意查收");
            }

            @Override
            public void failure(RestError error) {
                closeProgress();
                timer.cancel();
                ToastUtil.showToastShort(error.msg);
            }
        });
    }

    @Click(R.id.btnSave)
    public void btnRegister() {
        L.d("注册");
        String loginName = inputLoginName.getText().toString();
        if (TextUtils.isEmpty(loginName)) {
            ToastUtil.showToastShort("请输入用户名");
            return;
        }
        String userPwd = inputPassword.getText().toString();
        if (TextUtils.isEmpty(userPwd)) {
            ToastUtil.showToastShort("请输入密码");
            return;
        }
        String userPwdAgain = inputPasswordAgain.getText().toString();
        if (TextUtils.isEmpty(userPwdAgain)) {
            ToastUtil.showToastShort("请输入确认密码");
            return;
        }
        if (!userPwd.equals(userPwdAgain)) {
            ToastUtil.showToastShort("两次输入的密码不一致");
            return;
        }
        String mobile = inputPhone.getText().toString();
        if (TextUtils.isEmpty(mobile) || mobile.length() != 11) {
            ToastUtil.showToastShort("请输入正确的手机号码");
            return;
        }

        String smsCode = inputRadomNum.getText().toString();
        if (TextUtils.isEmpty(smsCode) || smsCode.length() != 6) {
            ToastUtil.showToastShort("请输入短信验证码");
            return;
        }
        String rel_user = inputRecommondId.getText().toString();
        String qq = inputQQ.getText().toString();
        RegisterRequest request = new RegisterRequest(mobile, userPwd, smsCode, rel_user, qq);
        showProgress();
        UserManager.register(request, new RestCallBack<LoginResponse>() {
            @Override
            public void success(LoginResponse loginResponse) {
                ToastUtil.showToastShort("注册成功");
                finish();
                closeProgress();
            }

            @Override
            public void failure(RestError error) {
                closeProgress();
                ToastUtil.showToastShort(error.msg);
            }
        });
    }


    @AfterViews
    void initData() {
        showProgress();
        smsRgGroup.removeAllViews();
        smsRgGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                try {
                    selectSmsId = (Long) group.findViewById(checkedId).getTag();
                } catch (Exception e) {

                }

            }
        });

        mNavBar.setOnMenuClickListener(new NavBar.OnMenuClickListener() {
            @Override
            public void onLeftMenuClick(View view) {
                finish();
            }

            @Override
            public void onRightMenuClick(View view) {
            }
        });

        addJob(UserManager.getSmsSvrList(new RestCallBack<GetSmsListResponse>() {
            @Override
            public void success(GetSmsListResponse response) {
                closeProgress();
                if (response.list == null || response.list.size() == 0) {
                    return;
                }
                for (SmsListInfo tmp : response.list) {
                    smsRgGroup.addView(getSmsRadioButton(tmp.name, tmp.smsid));
                }
                smsRgGroup.check(1);
            }

            @Override
            public void failure(RestError error) {
                closeProgress();
                ToastUtil.showToastShort(error.msg);
            }
        }));
    }

    /**
     * 倒计时
     */
    private CountDownTimer timer = new CountDownTimer(60000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            long time = millisUntilFinished / 1000;
            getSmsCodeView.setText("再次获取(" + time + "s)");
            getSmsCodeView.setClickable(false);
//            getSmsCodeView.setBackgroundResource(R.drawable.round_gray_normal_m_radius);
        }

        @Override
        public void onFinish() {
            getSmsCodeView.setText("再次获取");
            getSmsCodeView.setClickable(true);
//            getSmsCodeView.setBackgroundResource(R.drawable.xml_green_btn_selector_m);
        }
    };

    private RadioButton getSmsRadioButton(String name, long smsId) {
        RadioButton tempButton = new RadioButton(getActivity());
        tempButton.setButtonDrawable(R.drawable.selector_radiobox_red);
        tempButton.setText(name);
        tempButton.setTag(smsId);
        return tempButton;
    }
}
